#include "hplczkyspumpparsepacket.h"
#include "hplczkyspumpprotocoldata.h"
#include "common/hplczkyspfcid.h"
#include "common/hplczkyspacketformat.h"

#include <QVariant>
#include "modulehplccmdid.h"

namespace hplc
{
namespace zkys
{

HplcZkysPumpParsePacket::HplcZkysPumpParsePacket(HplcZkysPumpProtocolData &data, hplc::PumpParam &param, QObject *parent)
    : HplcZkysParseCommonPacket (data, param, parent)
{

}

HplcZkysPumpParsePacket::~HplcZkysPumpParsePacket()
{

}

void HplcZkysPumpParsePacket::parseSet(quint8 pfc, char *data, int len)
{
    PumpParam *param = (PumpParam*)(m_param);
    switch (pfc)
    {
    case PFC_PUMP_FLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        param->basicParam.rate = flow;

        QVariant var;
        var.setValue(param->basicParam.rate);
        emit parseCompleted(hplc::PumpCmdID_SetFlow, var);
    }
        break;
    case PFC_PUMP_FLOW_PERCENT:
    {
        param->basicParam.ratePercent = data[0];

        QVariant var;
        var.setValue(param->basicParam.ratePercent);
        emit parseCompleted(hplc::PumpCmdID_SetFlowPercent, var);
    }
        break;
    case PFC_PUMP_PRESS_MIN:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureMin = press;

        QVariant var;
        var.setValue(param->basicParam.pressureMin);
        emit parseCompleted(hplc::PumpCmdID_SetPressureMin, var);
    }
        break;
    case PFC_PUMP_PRESS_MAX:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureMax = press;

        QVariant var;
        var.setValue(param->basicParam.pressureMax);
        emit parseCompleted(hplc::PumpCmdID_SetPressureMax, var);
    }
        break;
    case PFC_PUMP_PRESS_WARNING:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressureWarning = press;

        QVariant var;
        var.setValue(param->basicParam.pressureWarning);
        emit parseCompleted(hplc::PumpCmdID_SetPressureWarning, var);
    }
        break;
    case PFC_PUMP_START_STOP:
    {
        param->basicParam.isStart = data[0];

        QVariant var;
        var.setValue(param->basicParam.isStart);
        emit parseCompleted(hplc::PumpCmdID_SetStartStop, var);
    }
        break;
    case PFC_PUMP_PAUSE_RESTORE:
    {
        param->basicParam.isPause = data[0];

        QVariant var;
        var.setValue(param->basicParam.isPause);
        emit parseCompleted(hplc::PumpCmdID_SetPauseRestore, var);
    }
        break;
    case PFC_PUMP_PURGE:
    {
        param->basicParam.isPurge = data[0];

        QVariant var;
        var.setValue(param->basicParam.isPurge);
        emit parseCompleted(hplc::PumpCmdID_SetPurge, var);
    }
        break;
    case PFC_PUMP_PURGE_FLOW:
    {
        quint32 temp = PTR2U32(data);
        float flow = *(float*)(&temp);
        param->basicParam.purgeRate = flow;

        QVariant var;
        var.setValue(param->basicParam.purgeRate);
        emit parseCompleted(hplc::PumpCmdID_SetPurgeFlow, var);
    }
        break;
    case PFC_PUMP_PURGE_TIME:
    {
        param->basicParam.purgeTime = data[0];

        QVariant var;
        var.setValue(param->basicParam.purgeTime);
        emit parseCompleted(hplc::PumpCmdID_SetPurgeTime, var);
    }
        break;
    case PFC_PUMP_PRESS_ZERO:
    {
        quint32 temp = PTR2U32(data);
        float voltage = *(float*)(&temp);
        param->basicParam.pressureZeroVoltage = voltage;

        QVariant var;
        var.setValue(param->basicParam.pressureZeroVoltage);
        emit parseCompleted(hplc::PumpCmdID_SetPressZero, var);
    }
        break;
    case PFC_PUMP_PRESS_UPLOAD_FREQ:
    {
        param->basicParam.pressureUploadFrequency = data[0];

        QVariant var;
        var.setValue(param->basicParam.pressureUploadFrequency);
        emit parseCompleted(hplc::PumpCmdID_SetPressUploadFreq, var);
    }
        break;
    case PFC_PUMP_FLOW_PRESS_COMPEN:
    {
        param->basicParam.isUsedPressureCompensation = data[0];

        QVariant var;
        var.setValue(param->basicParam.isUsedPressureCompensation);
        emit parseCompleted(hplc::PumpCmdID_SetFlowPressCompen, var);
    }
        break;
    case PFC_PUMP_MODE:
    {
        param->basicParam.mode = (PumpMode)data[0];

        QVariant var;
        var.setValue(param->basicParam.mode);
        emit parseCompleted(hplc::PumpCmdID_SetMode, var);
    }
        break;
    case PFC_PUMP_PRESS:
    {
        quint32 temp = PTR2U32(data);
        float press = *(float*)(&temp);
        param->basicParam.pressure = press;

        QVariant var;
        var.setValue(param->basicParam.pressure);
        emit parseCompleted(hplc::PumpCmdID_SetPressure, var);
    }
        break;
    case PFC_PUMP_RUNMODE:
    {
        param->basicParam.runMode = (PumpRunMode)data[0];

        QVariant var;
        var.setValue(param->basicParam.runMode);
        emit parseCompleted(hplc::PumpCmdID_SetRunMode, var);
    }
        break;
    // 恒压泵专用
    case PFC_PUMP_CP_FLOW_MAX:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.rateMax = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.rateMax);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_FlowMax, var);
    }
        break;
    case PFC_PUMP_CP_FLOW_MIN:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.rateMin = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.rateMin);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_FlowMin, var);
    }
        break;
    case PFC_PUMP_CP_PRESS_THRESHOLD:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.pressureThreshold = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.pressureThreshold);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_PressThreshold, var);
    }
        break;
    case PFC_PUMP_CP_PRESS_SET:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.pressure = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.pressure);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_Press, var);
    }
        break;
    case PFC_PUMP_CP_PID:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.pid.p = *(float*)(&temp);
        temp = PTR2U32(data + 4);
        param->constPressureParam.pid.i = *(float*)(&temp);
        temp = PTR2U32(data + 8);
        param->constPressureParam.pid.d = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.pid);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_PID, var);
    }
        break;
    case PFC_PUMP_CP_PRESS_PIDOUT:
    {
        quint32 temp = PTR2U32(data);
        param->constPressureParam.current.pressure = *(float*)(&temp);
        temp = PTR2U32(data + 4);
        param->constPressureParam.current.pidOut = *(float*)(&temp);

        QVariant var;
        var.setValue(param->constPressureParam.current);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_PressPidOut, var);
    }
        break;
    case PFC_PUMP_CP_PRESS_PIDOUT_UPLOAD_FREQ:
    {
        param->constPressureParam.pressurePidOutUploadFreq = data[0];

        QVariant var;
        var.setValue(param->constPressureParam.pressurePidOutUploadFreq);
        emit parseCompleted(hplc::PumpCmdID_Set_CP_PressPidOutUploadFreq, var);
    }
        break;

    case PFC_PUMP_EXT_SYSTEM:
    {
        HplcZkysPumpProtocolData *m_d = static_cast<HplcZkysPumpProtocolData*>(this->m_d);
        m_d->m_systemParser->parseSet(data[0], data + 1, len - 1);
    }
        break;
    case PFC_PUMP_EXT_USER:
        break;
    default:
        HplcZkysParseCommonPacket::parseSet(pfc, data, len);
        break;
    }
}

void HplcZkysPumpParsePacket::parseGet(quint8 pfc, char *data, int len)
{
    switch (pfc)
    {
    case PFC_PUMP_FLOW:
        break;
    case PFC_PUMP_FLOW_PERCENT:
        break;
    case PFC_PUMP_PRESS_MIN:
        break;
    case PFC_PUMP_PRESS_MAX:
        break;
    case PFC_PUMP_PRESS_WARNING:
        break;
    case PFC_PUMP_START_STOP:
        break;
    case PFC_PUMP_PAUSE_RESTORE:
        break;
    case PFC_PUMP_PURGE:
        break;
    case PFC_PUMP_PURGE_FLOW:
        break;
    case PFC_PUMP_PURGE_TIME:
        break;
    case PFC_PUMP_PRESS_ZERO:
        break;
    case PFC_PUMP_PRESS_UPLOAD_FREQ:
        break;
    case PFC_PUMP_FLOW_PRESS_COMPEN:
        break;
    case PFC_PUMP_MODE:
        break;
    case PFC_PUMP_PRESS:
        break;
    case PFC_PUMP_RUNMODE:
        break;
    // 恒压泵专用
    case PFC_PUMP_CP_FLOW_MAX:
        break;
    case PFC_PUMP_CP_FLOW_MIN:
        break;
    case PFC_PUMP_CP_PRESS_THRESHOLD:
        break;
    case PFC_PUMP_CP_PRESS_SET:
        break;
    case PFC_PUMP_CP_PID:
        break;
    case PFC_PUMP_CP_PRESS_PIDOUT:
        break;
    case PFC_PUMP_CP_PRESS_PIDOUT_UPLOAD_FREQ:
        break;

    case PFC_PUMP_EXT_SYSTEM:
    {
        HplcZkysPumpProtocolData *m_d = static_cast<HplcZkysPumpProtocolData*>(this->m_d);
        m_d->m_systemParser->parseGet(data[0], data + 1, len - 1);
    }
        break;
    case PFC_PUMP_EXT_USER:
        break;
    default:
        HplcZkysParseCommonPacket::parseGet(pfc, data, len);
        break;
    }
}


} // zkys
} // hplc
